// miniprogram/pages/books-city/books-city.js
const { bookDetail } = require('../../utils/book_detail')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    placholderValue: '',
    currentTab: 0,
    secondCurrentTab: '0',
    thirdCurrentTab: '0',
    currentTabList: [
      { id: '0', value: '分类' },
      { id: '1', value: '榜单' },
      { id: '2', value: '书单' }
    ],
    secondCurrentTabList: [
      { id: '0', value: '玄幻' },
      { id: '1', value: '武侠' },
      { id: '2', value: '都市' },
      { id: '3', value: '历史' },
      { id: '4', value: '科幻' },
      { id: '5', value: '悬疑' }
    ],
    thirdCurrentTabList: [
      { id: '0', value: '最新' },
      { id: '1', value: '最热' },
      { id: '2', value: '评分' },
      { id: '3', value: '完结' }
    ],
    books: [],
    clientHeight: ''
  },
  search: function () {
    wx.navigateTo({
      url: '../search/search?value=' + this.data.placholderValue
    })
  },
  swiperChange: function(e) {
    this.setData({
      currentTab: e.detail.current
    })
  },
  navClick: function(e) {
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  secondNavClick: function(e) {
    if (this.data.secondCurrentTab === e.target.dataset.index) {
      return false;
    } else {
      this.setData({
        secondCurrentTab: e.target.dataset.index
      })
    }
    let booksList = bookDetail.filter(item => {
      return item.about.type.indexOf(e.target.dataset.type) !== -1
    })
    this.setData({
      books: booksList
    })
  },
  thirdNavClick: function(e) {
    if (this.data.thirdCurrentTab === e.target.dataset.index) {
      return false;
    } else {
      this.setData({
        thirdCurrentTab: e.target.dataset.index
      })
    }
  },
  detailClick: function(e) {
    let id = e.currentTarget.dataset['id']
    wx.navigateTo({
      url: '../detail/detail?id=' + id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    let booksList = bookDetail.filter(item => {
      return item.about.type.indexOf('玄幻') !== -1
    })
    wx.getSystemInfo({ 
        success: function (res) {
            that.setData({ 
                clientHeight: res.windowHeight - 80 ,
                books: booksList
            }); 
        } 
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let searchArray = ['遮天', '剑来', '完美世界', '诡秘之主', '善良的死神', '执魔', '从姑获鸟开始', '神墓', '全球高武', '雪中悍刀行']
    let searchIndex = Math.floor(Math.random() * 10)
    this.setData({
      placholderValue: searchArray[searchIndex]
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})